package com.roberto.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.roberto.main.Main;

public class Player extends Entity {
    public static final float SHIP_VELOCITY = 200;
    public int lives = 3;
    private Sprite playersShip;
    private Sprite[] dyingAnimation = new Sprite[2];
    private Sound playerKilled, shotSound;
    private int current;
    private long tick;

    public Player() {
        width = 51;
        height = 32;
        x = Main.WIDTH / 4;
        y = 70;
        playersShip = new Sprite(Main.megaTexture, 1, 205, (int) width, (int) height);
        playerKilled = Gdx.audio.newSound(Gdx.files.internal("res/sounds/explosion16.wav"));
        shotSound = Gdx.audio.newSound(Gdx.files.internal("res/sounds/shoot16.wav"));
        dyingAnimation[0] = new Sprite(Main.megaTexture, 1, 244, 58, 32);
        dyingAnimation[1] = new Sprite(Main.megaTexture, 60, 239, 50, 37);
    }

    public Sprite getSprite() {
        if (destroyed) {
            if (Main.getCurrentTime() - tick >= 50) {
                current = current == 0 ? 1 : 0;
                tick = Main.getCurrentTime();
            }
            dyingAnimation[current].setPosition(x, y);
            return dyingAnimation[current];
        }
        return playersShip;
    }

    public Sprite getAliveSprite() {
        return playersShip;
    }

    public float getXforBullet() {
        return x + width / 2 - 2.3f;

    }

    public void destroy() {
        destroyed = true;
        tick = Main.getCurrentTime();
        playerKilled.play(0.2f);

    }

    public void revive() {
        destroyed = false;
        x = Main.WIDTH / 4;

    }

    public Rectangle getBounds() {
        return playersShip.getBoundingRectangle();
    }

    public void update(float delta) {
        playersShip.setPosition(x, y);

    }

    public void shot() {
        shotSound.play(0.2f);
    }
}
